﻿var page = new lg.page({
    data: {
        Id: 0
    },
    mounted: function () {
        this.List();
    },
    methods: {
        List: function () {
            layui.use(['table', 'element', 'form'], function () {
                var table = layui.table;
                var form = layui.form;
                var element = layui.element  //元素
                page.laytable = table.render({
                    elem: '#TemplateList'
                    , url: '/SysApi/GetTemplates'
                    , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                    , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    , cols: [[
                        {
                            field: 'rownum', title: '#', width: 60, templet: function (d) {
                                return d.LAY_TABLE_INDEX + 1
                            }
                        },
                        {
                            field: 'Id', title: 'Id', width: 60
                        },
                        {
                            field: 'Name', title: '模板名称', width: 200
                        }
                        , {
                            field: 'Type', title: '模板类型', width: 150, templet: function (d) {
                                switch (d.Type) {
                                    case 0:
                                        return "通用版";
                                        break;
                                    case 1:
                                        return "电脑版";
                                        break;
                                    case 2:
                                        return "手机版";
                                        break;
                                };
                            }
                        }
                        , {
                            field: 'Model', title: '模板类别', width: 150, templet: function (d) {
                                switch (d.Model) {
                                    case 0:
                                        return "公用"
                                        break;
                                    case 1:
                                        return "列表页"
                                        break;
                                    case 2:
                                        return '详情页'
                                        break;
                                    default:
                                        return '';
                                        break;
                                };
                            }
                        }
                        , {
                            field: 'Intro', title: '模板描述'
                        }
                        , {
                            field: 'IsHome', title: '首页模板', width: 120, align: "center", templet: function (d) {
                                if (d.Type > 0) {
                                    return laySwitch.render(d.IsHome, 'home')
                                } else {
                                    return '';
                                }
                                
                            }
                        }
                        , {
                            field: 'IsUsing', title: '是否启用', width: 120, align: "center", templet: function (d) {
                                return laySwitch.render(d.IsUsing, 'use')
                            }
                        }
                        , { fixed: 'right', title: '操作', toolbar: '#btTpl', width: 160 }
                    ]],
                    page: true
                });
                //头工具栏事件
                table.on('toolbar(TemplateList)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addTemplate':
                            loadTpl({
                                url: "/Lgm/AddTemplate",
                                target: "#floatBody"
                            })
                            return false;
                            break;
                        case 'refresh':
                            page.laytable.reload();
                            break;
                        case 'clone':
                            layer.confirm('您确定要全部备份吗？', function (lay) {
                                layer.close(lay);
                                lg.request({
                                    url: "/SysApi/backupTemplate",
                                    method: "post",
                                    success: function (d) {

                                        if (d.code == 0) {
                                            layer.msg("备份成功!", { icon: 1 });
                                        } else {
                                            layer.msg(d.msg, { icon: 2 })
                                        }
                                    }
                                });
                            });
                            break;
                    };
                });
                //监听行工具事件
                table.on('tool(TemplateList)', function (obj) {
                    var data = obj.data;
                    //console.log(obj)
                    switch (obj.event) {
                        case 'del':
                            layer.confirm('确定要删除模板[' + data.Name + ']吗?', function (index) {
                                lg.request({
                                    url: "/SysApi/DeleteTemplate",
                                    method: "post",
                                    data: { ids: data.Id },
                                    success: function (d) {
                                        if (d.code == 0) {
                                            layer.msg("删除成功!", { icon: 1 });
                                            page.laytable.reload();
                                        } else {
                                            layer.msg(d.msg, { icon: 2 })
                                        }
                                    }
                                })
                            });
                            break;
                        case 'edit':
                            loadTpl({
                                url: "/Lgm/EditTemplate/" + data.Id,
                                target: "#floatBody"
                            })
                            break;
                        case 'clone':
                            layer.confirm('您确定要备份吗？', function (lay) {
                                layer.close(lay);
                                lg.request({
                                    url: "/SysApi/backupTemplate",
                                    method: "post",
                                    data: { ids: data.Id },
                                    success: function (d) {
                                        if (d.code == 0) {
                                            layer.msg("备份成功!", { icon: 1 });
                                        } else {
                                            layer.msg(d.msg, { icon: 2 })
                                        }
                                    }
                                });
                            });
                            break;
                        case 'use':
                            laySwitch.do({
                                obj: obj,
                                key: "IsUsing",
                                url: "/SysApi/EditTemplate",
                                refresh: true
                            })
                            break;
                        case 'home':
                            laySwitch.do({
                                obj: obj,
                                data: { "Id": data.Id, "Type": data.Type, "IsHome": data.IsHome==1?0:1 },
                                key: "IsHome",
                                url: "/SysApi/SetHomePage",
                                refresh: true
                            })
                            break;
                    }

                });
                form.on('submit(bt_Search)', function (data) {
                    page.laytable.reload({
                        where: data.field,
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    return false;
                })
            });
        },
        Add: function () {
            var form = layui.form;
            var upload = layui.upload
                , element = layui.element
                , layer = layui.layer;
            form.render();
            form.on("submit(AddTemplate)", function (d) {
                if (!d.field.IsUsing) {
                    d.field.IsUsing = 0
                }
                lg.request({
                    url: "/SysApi/AddTemplate",
                    data: d.field,
                    method: 'post',
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("添加成功！", { icon: 1 });
                            page.back(true);
                        } else {
                            layer.msg(d.msg, { icon: 2 })
                        }
                    }
                })
                return false;
            })
            //拖拽上传
            upload.render({
                elem: '#upImg'
                , url: '/SysApi/SmallImgUpLoad' //改成您自己的上传接口
                , done: function (res) {
                    if (res.state) {
                        layer.msg('上传成功', { icon: 1 });
                        $(this.elem).find(".viewImg").html("<hr><img src='" + res.filePath + "' />");
                        $("#ImageUrl").val(res.filePath);
                    } else {
                        layer.msg(d.msg, { icon: 2 })
                    }
                }
            });
        },
        Edit: function (id) {
            var form = layui.form;
            var upload = layui.upload
                , element = layui.element
                , layer = layui.layer;
            form.render();
            lg.request({
                url: "/SysApi/GetTemplate/"+id,
                success: function (d) {
                    if (d.code == 0) {
                        if (typeof d.data == "object") {
                            form.val("TemplateEditForm", d.data)
                            $(".viewImg").html("<hr><img src='" + d.data.ImageUrl + "' />")
                        }
                    }
                }
            })
            //拖拽上传
            upload.render({
                elem: '#upImg'
                , url: '/SysApi/SmallImgUpLoad' //改成您自己的上传接口
                , done: function (res) {
                    if (res.state) {
                        layer.msg('上传成功', { icon: 1 });
                        $(this.elem).find(".viewImg").html("<hr><img src='" + res.filePath + "' />");
                        $("#ImageUrl").val(res.filePath);
                    } else {
                        layer.msg(d.msg, { icon: 2 })
                    }

                }
            });
            form.on("submit(EditTemplate)", function (d) {
                if (!d.field.IsUsing) {
                    d.field.IsUsing = 0
                }
                lg.request({
                    url: "/SysApi/EditTemplate",
                    data: d.field,
                    method: 'post',
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("修改成功！", { icon: 1 });
                            page.back(true);
                        } else {
                            layer.msg(d.msg, { icon: 2 })
                        }
                    }
                })
                return false;
            })
        }
    },
})